package com.ktg.mes.md.service.impl;

import java.util.Arrays;
import java.util.List;
import com.ktg.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.ktg.mes.md.mapper.MdBatchHeaderMapper;
import com.ktg.mes.md.domain.MdBatchHeader;
import com.ktg.mes.md.service.IMdBatchHeaderService;



/**
 * 产品批次单Service业务层处理
 *
 * @author byteco
 * @date 2022-12-15
 */
@Service
public class MdBatchHeaderServiceImpl extends ServiceImpl<MdBatchHeaderMapper, MdBatchHeader> implements IMdBatchHeaderService
{




    /**
     * 查询产品批次单
     *
     * @param entity 产品批次单
     * @param page 分页对象
     * @return 产品批次单
     */
    @Override
    public IPage<MdBatchHeader> queryPageList(MdBatchHeader entity, Page<MdBatchHeader> page) {
        QueryWrapper<MdBatchHeader> lqw = new QueryWrapper<MdBatchHeader>(entity);
        lqw.orderByDesc("create_time");
        IPage<MdBatchHeader> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询产品批次单
     *
     * @param entity 产品批次单
     * @return 产品批次单
     */
    @Override
    public List<MdBatchHeader> queryList(MdBatchHeader entity) {
        QueryWrapper<MdBatchHeader> lqw = new QueryWrapper<MdBatchHeader>(entity);
        lqw.orderByDesc("create_time");
        return this.list(lqw);
    }

    public MdBatchHeader getInfo(Long id)
    {
        return this.getById(id);
    }


    /**
     * 新增产品批次单
     *
     * @param entity 产品批次单
     * @return 结果
     */
    @Override
    public MdBatchHeader insert(MdBatchHeader entity)
    {
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        this.save(entity);
        return entity;
    }

    /**
     * 修改产品批次单
     *
     * @param entity 产品批次单
     * @return 结果
     */
    @Override
    public boolean update(MdBatchHeader entity)
    {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(entity);
    }

    /**
     * 批量删除产品批次单
     *
     * @param recordIds 需要删除的产品批次单主键
     * @return 结果
     */
    @Override
    public boolean delete(Long[] recordIds)
    {

        return this.removeByIds(Arrays.asList(recordIds));
    }

    /**
     * 删除产品批次单信息
     *
     * @param recordId 产品批次单主键
     * @return 结果
     */
    @Override
    public boolean delete(Long recordId)
    {
        return this.removeById(recordId);
    }

    private LambdaQueryWrapper<MdBatchHeader> buildQueryWrapper(MdBatchHeader entity) {
        LambdaQueryWrapper<MdBatchHeader> lqw = Wrappers.lambdaQuery();
                    lqw.like(StringUtils.isNotBlank(entity.getItemName()), MdBatchHeader::getItemName, entity.getItemName());
                    lqw.eq(StringUtils.isNotBlank(entity.getSpecification()), MdBatchHeader::getSpecification, entity.getSpecification());
                    lqw.eq(StringUtils.isNotBlank(entity.getViscosity()), MdBatchHeader::getViscosity, entity.getViscosity());
                    lqw.eq(StringUtils.isNotBlank(entity.getTag()), MdBatchHeader::getTag, entity.getTag());
                    lqw.eq(StringUtils.isNotBlank(entity.getPackingType()), MdBatchHeader::getPackingType, entity.getPackingType());
                    lqw.eq(StringUtils.isNotBlank(entity.getBatchNo()), MdBatchHeader::getBatchNo, entity.getBatchNo());
                    lqw.eq(StringUtils.isNotBlank(entity.getPrefix()), MdBatchHeader::getPrefix, entity.getPrefix());
                    lqw.eq(entity.getStartNumber() != null, MdBatchHeader::getStartNumber, entity.getStartNumber());
                    lqw.eq(entity.getEndNumber() != null, MdBatchHeader::getEndNumber, entity.getEndNumber());
                    lqw.eq(entity.getQuantity() != null, MdBatchHeader::getQuantity, entity.getQuantity());
                    lqw.eq(entity.getProductDate() != null, MdBatchHeader::getProductDate, entity.getProductDate());
                    lqw.eq(entity.getValidDate() != null, MdBatchHeader::getValidDate, entity.getValidDate());
                    lqw.eq(StringUtils.isNotBlank(entity.getTestResult()), MdBatchHeader::getTestResult, entity.getTestResult());
        return lqw;
    }
}
